package com.unitech.drinks.controller;

import com.unitech.drinks.exception.BusinessException;
import com.unitech.drinks.exception.ErrorResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 业务异常的全局处理
 */
@Slf4j
@RestControllerAdvice
public class BusinessExceptionHandler {
    //---------- 处理业务异常 ----------
    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR)
    public ErrorResponse handleError(BusinessException e) {
        log.error("业务服务异常", e);
        return ErrorResponse
                .builder()
                .code(e.getErrorCode().getCode())
                .message(e.getMessage())
                .build();
    }
}
